package org.android.data;


import java.io.UnsupportedEncodingException;
import org.apache.http.protocol.HTTP;
import org.example.other.MyVolley;
import org.example.xlibs.R;
import android.widget.ImageView;
import com.android.volley.NetworkResponse;
import com.android.volley.Request.Method;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.Response.ErrorListener;
import com.android.volley.Response.Listener;
import com.android.volley.toolbox.HttpHeaderParser;
import com.android.volley.toolbox.StringRequest;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
/**
 * 从服务器下载数据（图片/JSON数据）
 * @author asu
 *
 */
public abstract class XVolleyUtil {

	//从服务器下载图片
	public static void displayImage(String url,ImageView image){
		ImageLoader imageLoader_new = ImageLoader.getInstance();
		
		imageLoader_new.displayImage(url, image, new DisplayImageOptions.Builder()
		                                         .showImageOnLoading(R.drawable.ic_stub)
		                                         .showImageForEmptyUri(R.drawable.ic_empty)
		                                         .showImageOnFail(R.drawable.ic_error)
		                                         .cacheInMemory(true)
		                                         .cacheOnDisc(true).considerExifParams(true)
		                                       // .displayer(new RoundedBitmapDisplayer(0))
		                                         .build(), null);
	}
	
	// 从服务器下载JSON数据
	public static void httpGet(String strUrl, Listener<String> listener,ErrorListener mErrorListener){
		RequestQueue queue = MyVolley.getRequestQueue();
		StringRequest myReq = new StringRequest(Method.GET, strUrl,
				listener, mErrorListener){
			//解决Json解析乱码问题
			@Override
			protected Response<String> parseNetworkResponse(
					NetworkResponse response) {
				
				    String string = null;
					try {
						string = new String(response.data, HTTP.UTF_8);
					} catch (UnsupportedEncodingException e) {
						e.printStackTrace();
					}
				
				return Response.success(string,HttpHeaderParser.parseCacheHeaders(response));
			}
		};

		queue.add(myReq);
	}
	
}
